package com.icss.ah.productForManager.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import com.icss.ah.base.action.BaseAction;
import com.icss.ah.base.bean.EasyuiDataGridJson;
import com.icss.ah.base.bean.Json;
import com.icss.ah.base.bean.SyncSelectTree;
import com.icss.ah.base.util.ResourceLoader;
import com.icss.ah.login.bean.UserInfoBean;

@Controller
@Scope("prototype")
public class ProductForManagerAction extends BaseAction {
	
	private final Logger errlog = Logger.getLogger("E");
	private final Logger infolog = Logger.getLogger("I");
	

	/**
	 * 前往商品管理列表页面
	 * @return
	 */
	public String toProductForManagerList(){
		return "toProductForManagerList";
	}
	
	
	
	/**
	 * 获取商品分类树
	 */
	public void getTree(){
		request = getRequest();
		//id 异步加载展开树时传到后台的id
		//tarEnt 入口编码，以编码为入口编码的节点作为根节点
		//ifAll 是否一次性展示所有数据，1是 0否
		String id = request.getParameter("id");
		id = id == null ? "" : id;
		String ifAll = request.getParameter("ifAll");
		ifAll = ifAll == null ? "0" : ifAll;
		
		String shopId=this.getUserInfo(getUserCodeFromSession()).getShopId();
		SyncSelectTree selectTree = new SyncSelectTree();
		selectTree.setId(id);
		selectTree.setIfAll(ifAll);
		selectTree.setShopId(shopId);
		List<SyncSelectTree> list = this.productForManagerService.getTree(selectTree);
		/*HashMap map = new HashMap();
		map.put("corpId", corpId);
		map.put("shopId", shopId);*/
		for (SyncSelectTree sst : list) {
			sst.setOpen(true);
			if(sst.getIsParent() && "0".equals(sst.getpId())){
				sst.setNocheck(true);
			}
		}
		str2Resp(JSONArray.fromObject(list).toString());
	}

	/**
	 * 查询商品分类参数列表
	 */
	public void queryProductForManagerList(){
		request = this.getRequest();
		session=this.getSession();
		String productTypeCode = request.getParameter("productTypeCode");
		productTypeCode = (productTypeCode == null || "".equals(productTypeCode)) ? "" : productTypeCode;
		
		if(session.containsKey("productTypeCode")){
			session.remove("productTypeCode");
		}
		session.put("productTypeCode", productTypeCode);
		
		String result = "";
		try {
			String userCode=getUserCodeFromSession();  //获取当前登录者登录名
			UserInfoBean userInfo=this.getUserInfo(userCode);  
			String shopId=userInfo.getShopId();  //获取店铺id
			String corpId=userInfo.getCorpId(); //从配置文件里取公众号id
			
			dataGridJson = new EasyuiDataGridJson();
			HashMap map = new HashMap();
			map.put("productTypeCode", productTypeCode);
			map.put("startNo",Integer.valueOf(getStartIndexMysql()));
			map.put("pageRows",Integer.valueOf(getEndIndexMysql()));
			map.put("shopId", shopId);
			System.out.println(map);
			List<Map> productList = new ArrayList();
			
			productList = this.productForManagerService.queryProductForManagerList(map);
			long sortCount = this.productForManagerService.queryProductForManagerCount(map);//查询总记录数
			dataGridJson.setRows(productList);
			dataGridJson.setTotal(sortCount);
			result = JSONObject.fromObject(dataGridJson).toString();
			infolog.info("ProductForManagerAction|店铺id：" + shopId + "，查询商品参数列表成功");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ProductForManagerAction|查询商品参数列表失败：",e);
		} finally {
			str2Resp(result);
		}
	}
	
	public void saveProductForManager(){
		jsonText=new Json();
		session=this.getSession();
		request=this.getRequest();
		String productTypeCode=request.getParameter("productTypeCode");
		//System.out.println(productTypeCode+"---saveProductForManager");		
		String userCode=getUserCodeFromSession();
		UserInfoBean userInfo=getUserInfo(userCode);
		String shopId=userInfo.getShopId();
		String corpId = userInfo.getCorpId();
		String productIds=request.getParameter("productIds");
		String[] ids=productIds.split(",");
		for (int i = 0; i < ids.length; i++) {
			if(!"".equals(ids[i])){
				HashMap map  = new HashMap();
				map.put("productId", ids[i]);
				map.put("corpId", corpId);
				map.put("shopId", shopId);
				map.put("productTypeCode", productTypeCode);
				this.productForManagerService.saveProductForManager(map);
			}
		}
		jsonText.setSuccess(true);
		str2Resp(JSONObject.fromObject(jsonText).toString());
		
	} 
	
	public void deleteProductForManager(){
		request=this.getRequest();
		jsonText=new Json();
		boolean result=false;
		try {
			String uuid=request.getParameter("uuid")==null?"":request.getParameter("uuid");	
			Map map=new HashMap();
			map.put("uuid", uuid);		
			result=this.productForManagerService.deletetProductForManager(map);
			infolog.info("ProductForManagerAction|deleteProductForManager");
		} catch (Exception e) {
			e.printStackTrace();
			errlog.error("ProductForManagerAction|deleteProductForManager",e);
		}
		
		if(result){
			jsonText.setSuccess(true);
			jsonText.setMsg("删除成功");
		}else{
			jsonText.setSuccess(false);
			jsonText.setMsg("删除失败");
		}
		str2Resp(JSONObject.fromObject(jsonText).toString());
		
	}
	
}
